home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 9177 < prev    next >
Encoding:
Text File  |  1996-08-05  |  5.2 KB  |  127 lines

  1. Newsgroups: comp.lang.c++
  2. Path: netcom.com!marnold
  3. From: marnold@netcom.com (Matt Arnold)
  4. Subject: Re: What is referencing good for?
  5. Message-ID: <marnoldDnJ271.K09@netcom.com>
  6. Organization: NETCOM On-line Communication Services (408 261-4700 guest)
  7. References: <4goojd$a9g@wintermute.ecs.fullerton.edu>
  8. Date: Thu, 29 Feb 1996 07:59:25 GMT
  9. Sender: marnold@netcom23.netcom.com
  10.  
  11. grosin@titan.fullerton.edu (Gil Rosin) writes:
  12.  
  13. >I've been playing with referencing all day, and I can not find one use for it
  14. >that I can not do with pointers. What is referencing good for? I know about
  15. >reference parameters and I know about reference functions.
  16.  
  17. >The only pro I found about reference parameters is that it gives cleaner code,
  18.  
  19. [code snipped]
  20.  
  21. >Aside from that, I found referencing to have the same uses as pointers, if I
  22. >need to pass huge objects I can pass the address instead of the entire data,
  23. >but I can do this with pointers as well.
  24.  
  25. >I don't quite understand what reference functions are good for. Again, I can
  26. >do the same stuff with pointers.
  27.  
  28. References are functionally equivalent to pointers, they are implemented
  29. basically as pointers are and, yes, they give cleaner code in situations
  30. where pointers probably wouldn't have.
  31.  
  32. One major usage difference between pointers and references is that 
  33. references may only be initialized *once*.  Plus, you have to go out of
  34. your way to initialize a reference to refer to an invalid object.  In
  35. this simply way, references give the functionaity of pointers, but in a
  36. slightly "safer" way.  Plus, they give cleaner code to boot. 
  37.  
  38. Pointers, on the other hand, can be initialized as many times as you like
  39. (and with as many bogus values as you like).  The compiler cannot help you
  40. to put the right address into a pointer variable (it's perfectly legal C++
  41. to set a pointer to "null" or some other "non-address" and then try to 
  42. dereference it).
  43.  
  44. >Can someone give me a REAL world example of where I would use referencing
  45. >perhaps somewhere that I can not use pointers? 
  46.  
  47. When you want to write code that ensures that once something is set to
  48. point (refer) to something, is can only *ever* point to that something.
  49.  
  50. >And keep in mind that I'm a newbie, I'm just trying to learn the basics
  51. >right now.
  52.  
  53. It's might be hard to appreciate references until you appreciate all the 
  54. trouble it's possible to get into with pointers.  ;-)
  55.  
  56.  
  57. But, there's much more to it than that...
  58.  
  59. Reference parameters can be very important to C++ code (which is why they
  60. were added).  Consider, for example, a class with an assigment member (an
  61. operator= member function).  If this member took a non-reference/non-pointer 
  62. parameter...
  63.  
  64.    Object& Object::operator=(Object& obj)
  65.       {
  66.       // copy obj
  67.  
  68.       return *this;
  69.       } 
  70.  
  71. ...the object passed is an "obj" will be passed *by value* every time!  What
  72. if Object's have 5 megabytes of state data?  Obviously, you don't want to
  73. pass such large objects by value, especially when you're going to copy thema
  74. second time on purpse---you'd rather pass a reference.  Or, in your case you'd
  75. rather pass a pointer.  But, no wait!  If Object::operator=() were declared
  76. like this...
  77.  
  78.    Object& Object::operator=(Object* pobj);
  79.  
  80. ...the you'd have to call it like this...
  81.  
  82.    Object obj1; 
  83.    Object obj2;
  84.  
  85.    obj1 = &obj2;
  86.  
  87. ...which doesn't really "look" like what it does.  You want to write
  88. code that says, "Make obj1 equal to obj2".  The code above, taken literally,
  89. says "Make obj1 equal to the address of obj2", which really doesn't exress
  90. what is really happening.
  91.  
  92. You'd rather (I'd hope), want to write...
  93.  
  94.    obj1 = obj2;
  95.  
  96. ...but now obj2 will be copied onto the stack to call operator=().  What
  97. to do?  Why, use a reference parameter, of course!
  98.  
  99. C++ references let the details of whether of not something is to be passed
  100. by, well, reference to be handled by the callee, as opposed to the caller.
  101. As you can see, in C++, this can be very important.  With regular pointer
  102. parameters, the *caller* always has to know to manually pass an address.  In 
  103. C++, this can lead to code that looks a bit funny.
  104.  
  105. There are many other examples like this in C++ programming where references
  106. are the correct way to do something.  True, you can do it with pointers,
  107. but the result isn't often what C++ is supposed to be.
  108.    
  109. Did that help?
  110.  
  111.  
  112. -------------------------------------------------------------------------
  113. Matt Arnold                       |        | ||| | |||| |  | | || ||
  114. marnold@netcom.com                |        | ||| | |||| |  | | || ||
  115. Boston, MA                        |      0 | ||| | |||| |  | | || ||
  116. 617.389.7384 (h) 617.576.2760 (w) |        | ||| | |||| |  | | || ||
  117. C++, MIDI, Win32/95 developer     |        | ||| 4 3 1   0 8 3 || ||
  118. -------------------------------------------------------------------------
  119. Regards,
  120. -------------------------------------------------------------------------
  121. Matt Arnold                       |        | ||| | |||| |  | | || ||
  122. marnold@netcom.com                |        | ||| | |||| |  | | || ||
  123. Boston, MA                        |      0 | ||| | |||| |  | | || ||
  124. 617.389.7384 (h) 617.576.2760 (w) |        | ||| | |||| |  | | || ||
  125. C++, MIDI, Win32/95 developer     |        | ||| 4 3 1   0 8 3 || ||
  126. -------------------------------------------------------------------------
  127.